package iotwisp.school.user.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import iotwisp.school.user.command.login_token.CreateLoginTokenDCmd;
import iotwisp.school.user.command.login_token.ExpireLoginTokenDCmd;
import iotwisp.school.user.command.login_token.LogoutLoginTokenDCmd;
import iotwisp.base.orm.StringIdBaseEntity;
import iotwisp.base.util.UUIDGenerator;
import org.hibernate.annotations.DynamicUpdate;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * {@link diance.view.organization.provider.qo.LoginTokenViewQO}
 * {@link jzjd.organization.service.LoginTokenService} LoginTokenViewSPI
 * LoginTokenService LoginTokenViewService LoginTokenController
 * LoginTokenViewController
 * 
 * @author yuxx
 *
 */
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicUpdate
@Entity
@Table(name = M.TABLE_PREFIX + "login_token")
@SuppressWarnings("serial")
public class LoginToken extends StringIdBaseEntity {

	/**
	 * 登录主体id
	 */
	@Column(name = "subject_id")
	private String subjectId;

	/**
	 * 登录令牌全文
	 */
	@Column(name = "token", length = 2048)
	private String token;

	/**
	 * 创建时间
	 */
	@Column(name = "create_date", columnDefinition = M.DATE_COLUMN)
	private Date createDate;

	/**
	 * 登出时间
	 */
	@Column(name = "logout_date", columnDefinition = M.DATE_COLUMN)
	private Date logoutDate;

	/**
	 * 失效时间
	 */
	@Column(name = "expire_date", columnDefinition = M.DATE_COLUMN)
	private Date expireDate;

	/**
	 * 登录的客户端标识
	 */
	@Column(name = "client_id")
	private String clientId;

	/**
	 * 登录的客户端类型
	 */
	@Column(name = "client_type")
	private String clientType;

	/**
	 * 登录ip
	 */
	@Column(name = "ip")
	private String ip;

	public void create(CreateLoginTokenDCmd cmd, String token) {

		setId(UUIDGenerator.getUUID());
		setSubjectId(cmd.getSubjectId());
		setExpireDate(cmd.getExpireDate());
		setCreateDate(new Date());
		setClientId(cmd.getClientId());
		setClientType(cmd.getClientType());
		setIp(cmd.getIp());
		setToken(token);
	}

	public void expire(ExpireLoginTokenDCmd cmd) {

		setExpireDate(new Date());
	}

	public void logout(LogoutLoginTokenDCmd cmd) {

		setLogoutDate(new Date());
	}

}
